package com.bw.gmall.api.controller;

import com.bw.gmall.api.model.*;
import com.bw.gmall.api.service.StatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/stats")
@CrossOrigin
public class StatsController {
/*
* StatsController：统计接口控制器，提供 RESTful API 接口，
* 接收前端请求并调用StatsService获取数据，返回给前端。
* 接口包括流量统计、商品流量 TopN、关键词 TopN、用户画像等。
* */
    @Autowired
    private StatsService statsService;

    @GetMapping("/traffic")
    public List<TrafficStats> getTrafficStats(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getTrafficStatsByTimeRange(startTime, endTime);
        }
        return statsService.getTrafficStats();
    }

    @GetMapping("/goods-traffic/topn")
    public List<GoodsTrafficStats> getGoodsTrafficTopN(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getGoodsTrafficByTimeRange(startTime, endTime);
        }
        return statsService.getGoodsTrafficTopN(limit);
    }

    @GetMapping("/keyword/topn")
    public List<KeywordStats> getKeywordTopN(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getKeywordByTimeRange(startTime, endTime);
        }
        return statsService.getKeywordTopN(limit);
    }
    
    @GetMapping("/traffic-source/topn")
    public List<TrafficSourceStats> getTrafficSourceTopN(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getTrafficSourceByTimeRange(startTime, endTime);
        }
        return statsService.getTrafficSourceTopN(limit);
    }
    
    @GetMapping("/page-traffic/topn")
    public List<PageTrafficStats> getPageTrafficTopN(
            @RequestParam(defaultValue = "10") int limit,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getPageTrafficByTimeRange(startTime, endTime);
        }
        return statsService.getPageTrafficTopN(limit);
    }
    
    @GetMapping("/user-profile")
    public List<UserProfileStats> getUserProfileStats(
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime) {
        if (startTime != null && endTime != null) {
            return statsService.getUserProfileStatsByTimeRange(startTime, endTime);
        }
        return statsService.getUserProfileStats();
    }

    @GetMapping("/health")
    public String health() {
        return "GMall API 02 is running";
    }
}